function my_alert(text, callback) {
	$('#myalert').show();
	$('#myalert .myalert div').html(text);

	$('#my_ok').click(function() {
		$('#myalert').hide();

		if (typeof(callback) == 'function') {
			callback();
			return false;
		}
	});
}

function myalert_hide() {
	$('#myalert').hide();
}



$(function() {

    getBanner();
    showCmsIndex();
	$(".dropdown_menu a").hover(function() {
		$(this).css("color", "#4e6994")
		$(this).css("background", "#fff")
	}, function() {
		$(".dropdown_menu").find('a').css("background", "#4e6994")
		$(".dropdown_menu").find('a').css("color", "#fff")
	});
	$(".fz40 img").hover(function() {

		$(this).attr('src', 'images/shulan.png')
	}, function() {
		$(".fz40").find('img').attr('src', 'images/shubai.png')
	});

	$(".item .svg").hover(function() {
		$(this).addClass('zhuan')
	}, function() {
		$(this).removeClass('zhuan')
	});

	$(".readmore ").hover(function() {
		console.log(1)
		$(this).find('img').attr("src", "images/penu.png")
	}, function() {
		console.log(2)
		$(".readmore img").attr("src", "images/penb.png")
	});
	var swiper = new Swiper('.swiper-containerxxx', {
		slidesPerView: 4,
		loop: true,
		autoplay: {
			delay: 2000,
			disableOnInteraction: false,
		},
		spaceBetween: 30,
		pagination: {
			el: '.swiper-pagination',
			clickable: true,
		},
	});
	var swiper = new Swiper('.swiper-containerxx', {
		slidesPerView: 2,
		loop: true,
		autoplay: {
			delay: 2000,
			disableOnInteraction: false,
		},
		spaceBetween: 0,
		pagination: {
			el: '.swiper-pagination',
			clickable: true,
		},
	});

	// postion().top
	var tabsSwiper;
	tabsSwiper = new Swiper('.news-media .swiper-container', {
		speed: 500,
		onSlideChangeStart: function() {
			$(".news-media .tabs .active").removeClass('active');
			$(".news-media .tabs span").eq(tabsSwiper.activeIndex).addClass('active');
		}
	});
	$(".news-media .tabs span").on('touchstart mousedown', function(e) {
		e.preventDefault()
		$(".news-media .tabs .active").removeClass('active');
		$(this).addClass('active');
		tabsSwiper.swipeTo($(this).index());
	});
	$(".news-media .tabs span").click(function(e) {
		e.preventDefault();
	});
	var tabsSwiper2;
	tabsSwiper2 = new Swiper('.whrl .swiper-container', {
		speed: 500,
		onSlideChangeStart: function() {
			$(".whrl .tabs .active").removeClass('active');
			$(".whrl .tabs span").eq(tabsSwiper2.activeIndex).addClass('active');
		}
	});
	$(".whrl .tabs span").on('touchstart mousedown', function(f) {
		f.preventDefault()
		$(".whrl .tabs .active").removeClass('active');
		$(this).addClass('active');
		tabsSwiper2.swipeTo($(this).index());
	});
	$(".whrl .tabs span").click(function(f) {
		f.preventDefault();
	});
}); 

//end

$(".tabBar").slide({
	mainCell: ".conWrap",
	effect: "left",
	trigger: "click",
	pnLoop: false
});
$(".tabBar2").slide({
	mainCell: ".conWrap",
	effect: "left",
	trigger: "click",
	pnLoop: false
});

function bingsearch() {
	if ($("input#searchKeyword").val() == "") {
		my_alert('请输入关键字');
		return false;
	}
	var key = $("input#searchKeyword").val() + "+site:pku.edu.cn";
	var url = 'https://cn.bing.com/search?ensearch=1&q=' + encodeURI(key);
	window.open(url);
	hidelayer('#search')
	$("input#searchKeyword").val("");
}

function searchkeydown(e, but) {
	var currKey = 0,
		e = e || event;
	var _this = $(but);
	if (e.keyCode == 13) {
		_this.click();
		return false;
	}
}

function randomsort(a, b) {
	return Math.random() > .5 ? -1 : 1;
	//用Math.random()函数生成0~1之间的随机数与0.5比较，返回-1或1
}
$('#abab .list .item').hide();

function setdataL() {
	$('#abab .list').each(function(index) {
		var _that = $(this);
		var _lists = _that.find('.item');
		var total = _lists.size();
		var newArr = [];
		_lists.each(function(index) {
			if (total > 7) {
				if ($(this).is(':hidden')) {
					newArr.push(index);
				}
			} else {
				newArr.push(index);
			}
		})
		newArr.sort(randomsort);
		_lists.hide();
		var aaa = [];
		for (var i = 0; i < 4; i++) {
			var last = newArr.pop();
			aaa.push(last);
			var _item = _lists.eq(last);
			if (_item.find('.img').data('src')) {
				_item.find('.img').css({
					'background-image': 'url(' + _item.find('.img').data('src') + ')'
				});
				_item.find('.img').removeAttr('data-src');
			}
			_item.show();
		}
	})
}


var first = 0;
var svgs = [];
var svgs1 = [];

$(function() {
	$(".body_home").addClass("showdiv");
	setTimeout(function() {
		$(".body_home").addClass("s");
		// $(".home1").removeClass("showdiv");
	}, 500);
});

//$(".body_b").mCustomScrollbar({});
$(".home1 .td").each(function(index, element) {
	var s = GetRandomNum(5, 15);
	$(this).css("transition-delay", s * 0.1 + "s");
	$(this).find(".inner").css("transition-delay", (s + 3) * 0.1 + "s");
});


setInterval(function() {
	$(".home1 .td:has(strong)").removeClass("s");
	var s = GetRandomNum(0, $(".home1 .td:has(strong)").length);
	$(".home1 .td:has(strong)").eq(s).addClass("s");
}, 5000);



$(".td9,.td12,.td13,.td16,.td22").each(function(index, element) {
	var s = GetRandomNum(5, 15);
	$(this).css("animation-delay", s * 0.1 + "s");
});
$(".home4 .d").each(function(index, element) {
	var s = GetRandomNum(1, 7);
	$(this).css("transition-delay", s * 0.1 + "s");
	$(this).find("span").css("transition-delay", (s + 3) * 0.1 + "s");
});


$(".home2 .item").click(function() {
	$(this).siblings().removeClass("now");
	$(this).addClass("now");
	$(".home2 .pic .d").removeClass("s");
	$(".home2 .pic .d").eq($(this).index()).addClass("s");
});
$(".home2 .item").eq(0).click();

tabdiv(".home3 .title .fz40", ".home3 .list", "click", 3);
$(".home3 .title .fz40:first").click();

$(".rr").click(function() {
	$(".body_r").removeClass("dis");
	$('.body_r img').each(function(i) {
		if (!$(this).attr('src')) {
			$(this).attr('src', $(this).attr('data-src'))
			$(this).removeAttr('data-src');
		}
	})
	setdataL();

	$(".body_b").css("z-index", 1);
	$(".body_r").css("z-index", 3);
	$(".body_r").addClass("showdiv");
	$(".nav_b").addClass("showdiv");
	$(".rr").removeClass("showdiv");
	$(".home").addClass("lock");
	setTimeout(function() {
		$(".body_home").removeClass("showdiv");
		$(".home1").addClass("showdiv");
		//$(".home1 .td strong").each(function(index, element) {
		//shownum($(this),3000);
		//});
	}, 500);
	setTimeout(function() {
		first = 1;
	}, 1000);
});

$(".bb").click(function() {
	$(".body_b").addClass("showdiv"); //.focus();
	$(".header").addClass("mobile");
	//first=1;
	setTimeout(function() {
		$(".body_home").removeClass("showdiv"); //.addClass("leave");
		$(".home5").addClass("showdiv");
		//$(".nav_r").addClass("showdiv");
	}, 500);
})

$(".rb").click(function() {
	setTimeout(function() {
		$(".ll").click();
	}, 1100);
	if ($(".body_b").hasClass("showdiv")) {
		$(".body_r").removeClass("showdiv");
	} else {
		$(".bb").click();
		$(".body_b").css("z-index", 3);
		$(".body_r").css("z-index", 1);
	}
});

$(".body_b").scrollTop(0);

$(".body_home").mousewheel(function(event, delta, deltaX, deltaY) {
	if (first == 0) {
		$(".rr").click();
		setTimeout(function() {
			first = 1;
			$(".home").removeClass("showdiv"); //.addClass("leave");
		}, 1200);
	} else {
		if (delta < 0) {
			$(".bb").click();
		}
	}
});
// $(".home11 .item").mouseenter(function(){
//         if($(window).width()>=1024){
//             svgs[$(this).index()].reset().play();
//
//         }
//     }
// );
//
// $(".home11 .item").mouseenter(
//     function(){
//         svgs[$(this).index()].reset().play();
//     }
// );


var sw_k = 1;
document.onkeydown = function(event) {
	var e = event || window.event || arguments.callee.caller.arguments[0];
	if ((e.keyCode == 40 || e.keyCode == 32 || e.keyCode == 34) && $(".body_home").hasClass("showdiv")) {
		sw_k = 0;
		$(".bb").click();
		$(".body_b").animate({
			scrollTop: 0
		}, 10);
		$(".body_b").focus();
		setTimeout(function() {
			sw_k = 1;
		}, 1000);
	}
	if ((e.keyCode == 38 || e.keyCode == 33) && $(".body_b").hasClass("showdiv") && $(".body_b").scrollTop() == 0) {
		sw_k = 0;
		$(".body_home").addClass("showdiv").focus();
		setTimeout(function() {
			$(".banner .slick-active .a1").focus();
		}, 500);
		$(".body_b").removeClass("showdiv");
		$(".home5").removeClass("showdiv");
		if ($(".body_b").css("position") == "fixed") $(".header").removeClass("mobile");
		setTimeout(function() {
			sw_k = 1;
		}, 1000);
	}
	if ((e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 33) && $(".body_r").hasClass("showdiv") && sw == 1) {
		$(".nav_b .list .item.now").prev().click();
	}
	if ((e.keyCode == 39 || e.keyCode == 40 || e.keyCode == 34 || e.keyCode == 32) && $(".body_r").hasClass("showdiv") &&
		sw == 1) {
		$(".nav_b .list .item.now").next().click();
	}
}

$(window).scroll(function() {
	if ($(window).width() <= 850) {
		if ($(window).scrollTop() < 50) {
			$(".header").removeClass("mobile")
		}
		// else{
		//     $(".header").addClass("mobile")
		// }
	}
});

$(".ll").click(function() {
	$(".body_r").removeClass("showdiv");
	$(".nav_b").removeClass("showdiv");
	$(".rr").addClass("showdiv");
	$(".home").removeClass("lock")
	setTimeout(function() {
		$(".nav_b .item:first").click();
		$(".home1").removeClass("showdiv");
		if (!$(".body_b").hasClass("showdiv")) {
			$(".body_home").addClass("showdiv").focus();
		}
	}, 1100);
});

var video1 = document.getElementById("video1");

var gaodu = $(".home8").offset().top; //jq获取aaa元素距离body的距离

$('body').bind('mousewheel', function(e, delta) {
	 var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) ||  // chrome & ie
				(e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));              // firefox
		if (delta > 0) {
			// 向上滚 并且body_b滚动条为0时
			if(bh==0){
				$(".body_b").removeClass("showdiv");
				$(".header").removeClass("on");
			}
			
		} else if (delta < 0) {
			// 向下滚
			$(".body_b").addClass("showdiv");
			$(".header").addClass("on");
		}
	
	if ($(".home8").offset().top - $(document).scrollTop() < $(window).height()) {

		// console.log(1)
		$('.home8').addClass('showdiv')
	} else {
		// console.log(2)
		$('.home8').removeClass('showdiv')
	}
})

$(window).scroll(function() {
	$('body').bind('touchmove', function(e) {
		var  winHeight = $(window).scrollTop();
		var a = document.getElementById("home8").offsetTop;
		if (a >= $(window).scrollTop() && a < ($(window).scrollTop() + $(window).height())) {
			alert("div在可视范围");
		}
	});

});

 var  bh ="";
$(".body_b").scroll(function() {
	bh = $(this).scrollTop();
	$(".home6 .part1,.home8,.home9,.home11").each(function(index, element) {
		var e = $(this);
		var fix = parseInt(e.attr("fix"));
		// console.log(fix)
		if (!fix && fix != 0) {
			fix = $(window).height() * 6 * 0.1;
		} else {
			fix = $(window).height() * fix * 0.1;
		}
		// console.log(fix)
		//var h=$(window).height()-$(e).height()>0?$(window).height()-$(e).height():0;
		if ($(window).scrollTop() >= $(e).offset().top - fix) {
			if (!$(e).hasClass("showdiv")) {
				$(e).addClass("showdiv");
				if ($(e).hasClass("part1")) {
					setTimeout(function() {
						$(".home9 .part1").addClass("s");
					}, 1000);
				}
				if ($(e).find("#video1").length > 0) {
					video1.play();
				}
				if ($(e).parent().hasClass("home11")) {
					setTimeout(function() {
						for (var i = 0; i < svgs.length; i++) {
							svgs[i].play();
						}
					}, 500);
				}
			}
		} else {

			if ($(e).hasClass("showdiv")) {
				$(e).removeClass("showdiv");
				if ($(e).hasClass("part1")) {
					$(".home9 .part1").removeClass("s");
				}
			}
		}
	});
});

$(".body_b").mousewheel(function(event, delta, deltaX, deltaY) {
	if (delta > 0 && $(".body_b").scrollTop() == 0) {
		//$(".home").removeClass("leave");
		$(".body_home").addClass("showdiv");
		setTimeout(function() {
			if ($(window).width() > 850) {
				$(".banner .slick-active .a1").focus();
			}
			//$(".banner .slick-active .a1").focus();
		}, 500);
		$(".body_b").removeClass("showdiv");
		// $(".header").removeClass("mobile")
		// $(".home9").removeClass("showdiv");
		$(".home5").removeClass("showdiv");

		if ($(".body_b").css("position") == "fixed") $(".header").removeClass("mobile");
		//$(".nav_r").removeClass("showdiv");

	}

	if (deltaY < 0 && $(".body_b").scrollTop() + $(window).height() >= $(".body_b .scroll").height()) {
		//$(".rr").click();
		//setTimeout(function(){
		//$(".body_b").scrollTop(0);
		//},1250);
	}
	if (deltaY > 0) {
		$(".header2").removeClass("lit");
	}
	if (deltaY < 0) {
		$(".home9").addClass("showdiv");
		$(".header2").addClass("lit");
	}
});

//初始化文字
function settxt(e) {
	var text = $(e).text();
	var l = 0;
	var html = "";
	while (l < text.length) {
		html += "<i>" + text.substr(l, 1) + "</i>";
		l += 1;
	}
	$(e).html(html);
}

$(".home1 .td").each(function(index, element) {
	settxt($(this).find("strong"));
});

var ls = 100 / $(".home2 .list .item").length;
$(".home2 .list").mCustomScrollbar({
	autoDraggerLength: false,
	axis: 'x',
	scrollbarPosition: 'outside',
	callbacks: {
		whileScrolling: function() {
			//console.log(this.mcs.leftPct)
			var p = this.mcs.leftPct;
			$(".home2 .list .item").each(function(index, element) {
				if (p > index * ls) {
					$(this).siblings().removeClass("now");
					$(this).addClass("now");
				}
			});
		}
	}
})


tabdiv(".home8 .l .fz40 a", ".home8 .div .list", "click", 1, 300);
tabdiv(".home8 .l .fz40 a", ".home8 .l .a a", "click", 1, 300);
$(".home8 .l .fz40 a").eq(0).click();

//延迟加载
var sto_r = 0;
var loaded = 0;

//获取图片地址
var imgArr = [];
$('.banner .child').each(function() {
	var item = $(this);
	if (typeof(item.attr("src")) != "undefined" && item.attr("src") != "" && this.nodeName.toLowerCase() == "img") {
		imgArr.push({
			src: item.attr('src')
		});
	} else if (item.css("background-image") != "none") {
		imgArr.push({
			src: item.css("background-image").replace(/^url\(["']?/, '').replace(/["']?\)$/, '')
		});
	}
});

function _lazyload() {
	$(".body_b img,.body_b source").each(function(index, element) {
		if ($(this).attr("data-src") != "") {
			$(this).attr("src", $(this).attr("data-src"));
			$(this).removeAttr('data-src');
		}
	});
}

// //加载图片
// $.imgpreload(imgArr,{
//     each: function(){},
//     all: function(){
//         $(".body_b").removeClass("dis");
//         $(".rr").addClass("showdiv");
//         $(".home11 .item svg").each(function(index, element) {
//             var id=$(this).attr("id");
//             svgs[index] = new Vivus(id,{type:'delayed',duration:60});
//         });
//         $(".home7 svg").each(function(index, element) {
//             var id=$(this).attr("id");
//             svgs1[id] = new Vivus(id,{type:'delayed',duration:60,start:'autostart'});
//         });
//     }
// });

window.onload = function() {
	_lazyload();
}

function getCmsList(channel,pageNumber,pageSize,callback){
    var param = {
        channelId : channel,
        pageNumber:pageNumber,
        pageSize:pageSize
    }
    $.post(window.serviceUrl+"content/getContentList", param, callback, 'json');
}
function getCmsIndex(callback){
    var param = {
    }
    $.post(window.serviceUrl+"content/getCmsIndex", param, callback, 'json');
}
function getBanner(){
    getCmsList(window.CHANNEL.sylbt,1,10,function(data){
        if(data.rtnCode == "000000"){
            var list = data.responseData.records;
            var content = "";
            $.each(list, function(i, item){
                content += '<li>\n' +
                    '                    <a href="detail.html?id='+item.id+'&channel1=首页" target="_blank" title="">\n' +
                    '                        <img src="'+window.imgUrl+item.typesimgUrl+'" alt="">\n' +
                    '                    </a>\n' +
                    '                    <div class="banner-title"><a href=""></a>\n' +
                    '                    </div>\n' +
                    '                </li>'
            });
            $("#slider").html(content).responsiveSlides({
                auto: true,
                nav: true,
                speed: 500,
                timeout: 4000,
                pager: true,
              //  pause: true,
                pauseControls: true,
                namespace: "callbacks"
            });
        }
    });
}

function showCmsIndex(){
    getCmsIndex(function(data){
        if(data.rtnCode == "000000"){
            getXydt(data.responseData.xydtdt,data.responseData.xydtycbt);
            getJxky(data.responseData.jxky);
            getZtwz(data.responseData.ztwz);
            getXxzx(data.responseData.xxgk,data.responseData.jyzx);
            $("#xyfczt").find("img").attr("src",window.imgUrl+data.responseData.xyfcz[0].typesimgUrl).on("click",function(){
                window.open("detail.html?id="+data.responseData.xyfcz[0].id +"&channel1=校园.风采");
            });
           /* $("#xyfcyt").find("img").attr("src",window.imgUrl+data.responseData.xyfcy[0].typesimgUrl).on("click",function(){
                window.open("detail.html?id="+data.responseData.xyfcy[0].id +"&channel1=校园.风采");
            });*/
            $("#xyfczt").find("p").html(data.responseData.xyfcz[0].title);
           /* $("#xyfcyt").find("p").html(data.responseData.xyfcy[0].title);*/
            getXyfcsp(data.responseData.xyfcx);
        }
    });
}
function getXydt(list,list1){
    var content = "";
    $.each(list, function(i, item){
        content += '<li class="col-md-6 col-sm-6 col-xs-12">\n' +
            '                                                        <div class="item">\n' +
            '                                                            <div class="pic"><a href="detail.html?id='+item.id+'&channel1=学院动态"' +
            '                                                                    target="_blank" title="'+item.title+'"><img\n' +
            '                                                                        width="375" height="275" class="coverImg"\n' +
            '                                                                        alt="'+item.title+'"\n' +
            '                                                                        onerror="this.src=\'//images/nopic.jpg\'"\n' +
            '                                                                        src="'+window.imgUrl+item.typesimgUrl+'"></a>\n' +
            '                                                            </div>\n' +
            '                                                            <div class="text">\n' +
            '                                                                <h3><a href="detail.html?id='+item.id+'&channel1=学院动态" target="_blank"\n' +
            '                                                                        title="'+item.title+'">'+item.title+'</a></h3>\n' +
            '                                                                <div class="summary">'+item.description+'</div>\n' +
            '                                                            </div>\n' +
            '                                                        </div>\n' +
            '                                                    </li>';
    });
    $("#xydtdt").html(content);
    content = "";
    $.each(list1, function(i, item){
        content += '<li>\n' +
            '                                                    <div class="date">\n' +
            '                                                        <span class="d">'+item.releaseDate.substring(8,10)+'</span>\n' +
            '                                                        <span class="y">'+item.releaseDate.substring(0,7)+'</span>\n' +
            '                                                    </div>\n' +
            '                                                    <div class="text">\n' +
            '                                                        <h4><a href="detail.html?id='+item.id+'&channel1=学院动态" target="_blank">'+item.title +
            '                                                            </a></h4>\n' +
            '                                                        <p>'+item.description +'</p>\n' +
            '                                                    </div>\n' +
            '                                                </li>';
    });
    $("#xydtycbt").html(content);
}

function getJxky(list){
    var content = "";
    $.each(list, function(i, item){
        content += '<div class="l"><a target="_blank" href="detail.html?id='+item.id+'&channel1=教学科研">' +
            '<img class="_lazyload" src="'+window.imgUrl+item.typesimgUrl+'"></a></div>' +
            ' <div class="r" ><div class="h"><a target="_blank" href="detail.html?id='+item.id+'&channel1=教学科研">'+item.title +'</a>\n' +
            '</div><div class="p" ><p style="text-align: left">'+item.description  +
            '</p></div><div class="date"></div></div>';
    });
    $("#jxky").html(content);
}

function getZtwz(list){
    var content = "";
    $.each(list, function(i, item){
        content += '<div class="item"><div class="pic"'
            +'style="background-image:url('+window.imgUrl+item.typesimgUrl+');"></div>'
            +'<div class="tit">'
            +'  <div class="h">'+item.title+'</div>'
            +' </div>'
            +'  <span class="more1">查看更多</span> <a href="'+item.originUrl+'"target="_blank"></a>'
            +'</div>';
    });
    $("#ztwz").html(content);
}

function getXxzx(list,list2){
    var content = "";
    $.each(list, function(i, item){
        content += '<li>'
            +' <div class="date">'
            +' <span class="d">'+item.releaseDate.substring(8,10)+'</span>'
            +'  <span class="y">'+item.releaseDate.substring(0,7)+'</span>'
            +'  </div>'
            +' <div class="text">'
            +'  <h4><a href="detail.html?id='+item.id+'&channel1='+encodeURI('信息公开')+'" target="_blank">'+item.title+'</a></h4>'
            +' <p>'+item.description+'</p>'
            +' </div> </li>';
    });
    $("#xxgk").html(content);
    content = "";
    $.each(list2, function(i, item){
        content += '<li>'
            +' <div class="date">'
            +' <span class="d">'+item.releaseDate.substring(8,10)+'</span>'
            +' <span class="y">'+item.releaseDate.substring(0,7)+'</span>'
            +' </div>'
            +' <div class="text">'
            +'  <h4><a href="detail.html?id='+item.id+'&channel1=教育资讯" target="_blank">'+item.title+'</a></h4>'
            +' <p>'+item.description+'</p>'
            +' </div>'
            +' </li>';
    });
    $("#jyzx").html(content);
}

function getXyfcsp(list){
    var content = "";
    var colClass = "col-xs-12";
    if(bodyWidth<=768){
        colClass = "col-xs-6"
    }
    $.each(list, function(i, item){
    	content +='<div class="col-lg-4 col-md-4 col-sm-12 '+colClass+'">' +
            '<a target="_blank" href="detail.html?id='+item.id+'&channel1=校园.风采"><div class="page-header clearfix">' +
            '<div class="more pull-right">' +
            '<img src="'+window.imgUrl+item.typesimgUrl+'"/>' +
            '</div>\n' +
            '<p>'+item.title+'</p>' +
            '</div></a>' +
            '</div>';
    });
    $("#xyfcx").html(content);

}
// 移动端适配
var bodyWidth = document.body.clientWidth || document.documentElement.clientWidth;
if(bodyWidth<=768){
    $('.body_b').css({
        'position':"static",
        'padding-top':"0",
        "background-size":"auto"
    })
}